// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Create reinforcment groups
// -----------------------------------------------
if (!isServer) exitWith {};

private [
	"_position",
	"_arrayofvehicle",
	"_vehicle",
	"_group",
	"_leader",
	"_motorized",
	"_distancemax",
	"_cible",
	"_count",
	"_enemys",
	"_countposition",
	"_objects",
	"_enemyposition",
	"_unit",
	"_soldier",
	"_marker",
	"_markerdest",
	"_typeofgroup"
];

_marker      = _this select 0;
_markerdest  = _this select 1;
_typeofgroup = _this select 2;
_motorized   = _this select 3;

if (_motorized) then {
	_position = (position wcheavyfactory) findEmptyPosition [5, 50];
	if (_position select 0 == 0) then {
		diag_log "WARCONTEXT: CANNOT FIND EMPTY POSITION FOR SUPPORT GROUP VEHICLE SPAWN";
	};

	_arrayofvehicle = [_position, 0, _typeofgroup, east] call BIS_fnc_spawnVehicle;
	_vehicle = _arrayofvehicle select 0;
	_group   = _arrayofvehicle select 2;

	diag_log format ["WARCONTEXT: CREATING A SUPPORT VEHICLE %2 IN ZONE %1", _marker, _typeofgroup];

	//A3 TODO: setVehicleInit
	_vehicle setVehicleInit "this lock true; [this] spawn EXT_fnc_atot;";
	processInitCommands;

	_vehicle setDir (random 360);
	_vehicle setFuel wcenemyglobalfuel;

	wcgarbage = [_vehicle] spawn WC_fnc_vehiclehandler;
} else {
	_position = (position wcbarrack) findEmptyPosition [5, 50];
	if (_position select 0 == 0) then {
		diag_log "WARCONTEXT: CANNOT FIND EMPTY POSITION FOR UNITS SUPPORT GROUP SPAWN";
	};

	_group = [_typeofgroup, east, _position] call WC_fnc_popgroup;
	diag_log format ["WARCONTEXT: CREATING A SUPPORT GROUP %2 IN ZONE %1 OF SIZE %3", _marker, _typeofgroup, count (units _group)];
	
	_leader = leader _group;

	_position = (position wcbarrack) findEmptyPosition [5, 50];
	if (_position select 0 == 0) then {
		diag_log "WARCONTEXT: CANNOT FIND EMPTY POSITION FOR UNITS TRANSPORT SUPPORT GROUP SPAWN";
	} else {
		_vehicle = "BTR60_TK_EP1" createVehicle _position;
		wcgarbage = [_vehicle] spawn WC_fnc_vehiclehandler;
		_group addVehicle _vehicle;
	
		_leader moveInDriver _vehicle;
		{
			if (isNull (gunner _vehicle)) then {
				_x moveInGunner _vehicle;
			};
			_x moveInCargo _vehicle;
		} foreach (units _group);
	};
};

wcgarbage = [_group] spawn WC_fnc_grouphandler;
wcsupportgroup = wcsupportgroup + [_group];

// Initialisation script for units
_leader = leader _group;

_position = [_markerdest, "onground", "onflat"] call WC_fnc_createpositioninmarker;
while {((count (units _group) > 0) and (_leader distance _position > wcdistance))} do {
	_leader = leader _group;

	_group setBehaviour "COMBAT";
	_group setCombatMode "WHITE";
	_group setSpeedMode "FULL";

	if (vehicle _leader != _leader) then {_motorized = true; _vehicle = vehicle _leader} else {_motorized = false};
	if (_motorized) then {
		_count = 0;
		_distancemax = 1000;
		_enemys = (getPosATL _leader) nearEntities [["CAManBase", "LandVehicle"], 300];
		{
			if (side _x in wcside) then {
				if (_leader distance _x < _distancemax) then {_distancemax = _leader distance _x; _cible = _x};
				_count = _count + 1;
			};
		} foreach _enemys;

		{
			if (_count == 0) then {
				(driver _vehicle) doMove _position;
			} else {
				if(count (crew _vehicle) > 3) then {
					(driver _vehicle) action ["engineOff", _vehicle];
					_group leaveVehicle _vehicle;
					sleep 1;
					{
						_x action ["eject", _vehicle];
						_x reveal _cible;
						_x doTarget _cible;
						_x doFire _cible;
						_x doMove position _cible;
					} foreach (crew _vehicle);
				} else {
					(gunner _vehicle) reveal _cible;
					(driver _vehicle) reveal _cible;
					(commander _vehicle) reveal _cible;
					(gunner _vehicle) doTarget _cible;
					(gunner _vehicle) doFire _cible;
					(driver _vehicle) doMove position _cible;
				};
			};
		} foreach units _group;
	} else {
		_count = 0;
		_unit = leader _group;
		_distancemax = 1000;
		_enemys = (getPosATL _unit) nearEntities [["CAManBase", "LandVehicle"], 300];
		{
			if (vehicle _x == _x) then {
				if (side _x in wcside) then {
					_unit reveal _x;
					if (_unit distance _x < _distancemax) then {_distancemax = _unit distance _x; _cible = _x};
					_count = _count + 1;
				};
			} else {
				if (count (crew _x) > 0) then {
					if (side (driver _x) in wcside) then {
						_unit reveal _x;
						if (_unit distance _x < _distancemax) then {_distancemax = _unit distance _x; _cible = _x};
						_count = _count + 1;
					};
				};
			};
		} foreach _enemys;

		{
			_unit = _x;
			if (_count == 0) then {
				_unit doMove _position;
				_objects = nearestObjects [_unit, ["LandVehicle"], 100];
				{
					if (count (crew _x) == 0) then {
						_countposition = (_x emptyPositions "cargo") + (_x emptyPositions "driver") + (_x emptyPositions "commander") + (_x emptyPositions "gunner");
						if (_countposition > count (units _group)) then {
							_x setDamage 0;
							_x setFuel 1;
							_group addVehicle _x;
						};
					};
				} foreach _objects;
			} else {
				_unit doWatch _cible;
				_unit doTarget _cible;
				_unit doFire _cible;
				_unit reveal _cible;
				_enemyposition = ([position _cible, 10, 360, getDir _cible, 5] call WC_fnc_createcircleposition) call BIS_fnc_selectRandom;
				_unit doMove _enemyposition;
			};
		} foreach (units _group);
	};
	sleep 30;
};

if (count (units _group) == 0) exitWith {};

if (vehicle _leader != _leader) then {_motorized = true; _vehicle = vehicle _leader} else {_motorized = false};
if (_motorized) then {
	if (count (crew _vehicle) > 3) then {
		(driver _vehicle) action ["engineOff", _vehicle];
		_group leaveVehicle _vehicle;
		sleep 1;
		{
			_x action ["eject", _vehicle];
		} foreach (crew _vehicle);
		wcgarbage = [(leader _group), _markerdest, "noslow", "showmarker"] spawn EXT_fnc_upsmon;
		//wcgarbage = [_group, (position (leader _group)), wcdistance] spawn WC_fnc_patrol;

		//A3 TODO: Crew
		// After group leave vehicle, a new group drive the vehicle
		_group = createGroup east;
		_soldier = _group createUnit [(wccrewforces call BIS_fnc_selectRandom), [0,0], [], 0, "FORM"];
		_soldier moveInDriver _vehicle;

		_soldier = _group createUnit [(wccrewforces call BIS_fnc_selectRandom), [0,0], [], 0, "FORM"];
		_soldier moveInGunner _vehicle;

		_soldier = _group createUnit [(wccrewforces call BIS_fnc_selectRandom), [0,0], [], 0, "FORM"];
		_soldier moveInCommander _vehicle;
		_group addVehicle _vehicle;

		wcgarbage = [_group] spawn WC_fnc_grouphandler;
		wcgarbage = [_vehicle, _markerdest, "showmarker"] execVM "extern\ups.sqf";
	} else {
		//_scriptinit = format ["wcgarbage = [this, '%1', 'showmarker'] execVM 'extern\ups.sqf';", _markerdest];
		//_vehicle setVehicleInit _scriptinit;
		wcgarbage = [_vehicle, _markerdest, "showmarker"] execVM "extern\ups.sqf";
	};
} else {
	wcgarbage = [(leader _group), _markerdest, "noslow", "showmarker"] spawn EXT_fnc_upsmon;
	//wcgarbage = [_group, (position (leader _group)), wcdistance] spawn WC_fnc_patrol;
};